//
//  CollectController.m
//  SleepingElf
//
//  Created by karong on 2023/10/26.
//

#import "CollectController.h"
#import "CollectItemController.h"
#import "ZhuanShuController.h"

@interface CollectController ()<CollectItemControllerDelgate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSMutableDictionary *contentVCDict;
@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation CollectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIndex = 0;
    [self viewLayout];
    [self setContentVCWithIndex:self.selectIndex];
}

- (void)viewLayout {
    [self.scrollView setContentSize:CGSizeMake(kScreenWidth * 2, self.contentHeight)];
}

- (UIViewController *)addCollectItemController {
    CollectItemController * target = (CollectItemController *)[self.storyboard instantiateViewControllerWithIdentifier:@"CollectItemController"];
    target.delegate = self;
    target.view.frame = CGRectMake(0, 0, SEScreenWidth, self.contentHeight);
    return target;
}

- (UIViewController *)addZhuanShuItemController {
    ZhuanShuController * target = (ZhuanShuController *)[self.storyboard instantiateViewControllerWithIdentifier:@"ZhuanShuController"];
//    target.delegate = self;
    target.view.frame = CGRectMake(0, 0, SEScreenWidth, self.contentHeight);
    return target;
}

// 获取並且添加子控制器
- (void)setContentVCWithIndex:(NSInteger)index {
    BOOL animated = YES;
    if(self.contentVCDict.count == 0) {
        animated = NO;
    }
    UIViewController *vc = self.contentVCDict[@(index)];
    if(!vc) {
        if (index == 0) {
            vc = [self addCollectItemController];
        }
        else {
            vc = [self addZhuanShuItemController];
        }
        vc.view.frame = self.scrollView.bounds;
        vc.view.mj_x = SEScreenWidth * index;
        vc.view.mj_y = 0;
        [self.scrollView addSubview:vc.view];
        [self addChildViewController:vc];
        self.contentVCDict[@(index)] = vc;
    }
    [self.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:animated];
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat x = scrollView.contentOffset.x / SEScreenWidth;
        [self setContentVCWithIndex:x];
        self.selectIndex = x;
        [self.listView reloadData];
    }
}

#pragma mark - UICollectionViewDelete

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"kcell" forIndexPath:indexPath];
    UILabel * label = [cell viewWithTag:101];
    label.text = self.dataArray[indexPath.row];
    UIView * view = [cell viewWithTag:102];
    view.hidden = !(self.selectIndex == indexPath.row);
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString * title = self.dataArray[indexPath.row];
    CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, 0.0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14 weight:(UIFontWeightMedium)]} context:nil].size.width;
    return CGSizeMake(width + 24, 30);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
    [self setContentVCWithIndex:self.selectIndex];
    [self.listView reloadData];
}

#pragma mark - CollectItemControllerDelgate
- (void)collectItemControllerDidSelectWithObject:(MusicModel *)object {
    [[PlayerManager shared]playWithMusic:object];
}

- (NSMutableDictionary *)contentVCDict {
    if (!_contentVCDict) {
        _contentVCDict = [[NSMutableDictionary alloc]init];
    }
    return _contentVCDict;
}

- (CGFloat)contentHeight {
    if (_contentHeight <= 0) {
        _contentHeight = SEScreenWidth - self.view.safeAreaInsets.top - 20 - 30 - 44;
    }
    return _contentHeight;
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[WSLocalized(@"我的收藏"), WSLocalized(@"专属白噪音")];
    }
    return _dataArray;
}

@end
